﻿using System.IO;
using System.Text;
using ApprovalTests;
using ApprovalTests.Reporters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Templates.Models;

namespace Templates.UnitTests
{
    /// <summary>
    /// Set of tests, using data from an existing Legacy application
    /// </summary>
    [TestClass, UseReporter(typeof(DiffReporter))]
    public class LegacyTests
    {
        private readonly TemplateFormatter _formatter = new TemplateFormatter();

        private Template NewTemplate(string path)
        {
            return new Template
            {
                IgnoreCase = false,
                StartCharacter = "[",
                EndCharacter = "]",
                Body = File.ReadAllText(path),
            };
        }

        private string ToText(Template template)
        {
            StringBuilder builder = new StringBuilder();
            foreach (var item in template.Variables)
            {
                builder.Append(item.Name);
                builder.Append(": ");
                builder.AppendLine(item.Value);
            }
            return builder.ToString();
        }

        private string ReadWrite(Template template)
        {
            template = _formatter.Read(template);

            int count = 0;
            foreach (var item in template.Variables)
            {
                if (item.Name == "cisco.StoreNumber")
                {
                    item.Value = "123";
                }
                else
                {
                    item.Value = item.Name + " - " + ++count;
                }
            }

            return _formatter.Write(template);
        }

        [TestMethod]
        public void Cisco_1811_Router_Import()
        {
            Template template = NewTemplate(TestConstants.Cisco1811);

            template = _formatter.Read(template);

            Approvals.Approve(ToText(template));
        }

        //Removed for now
        //[TestMethod]
        //public void Cisco_1811_Router_Write()
        //{
        //    Template template = NewTemplate(TestConstants.Cisco1811);

        //    string text = ReadWrite(template);

        //    Approvals.Approve(text);
        //}

        [TestMethod]
        public void Cisco_871_Router_Import()
        {
            Template template = NewTemplate(TestConstants.Cisco871);

            template = _formatter.Read(template);

            Approvals.Approve(ToText(template));
        }

        [TestMethod]
        public void Cisco_881_Router_Import()
        {
            Template template = NewTemplate(TestConstants.Cisco881);

            template = _formatter.Read(template);

            Approvals.Approve(ToText(template));
        }

        [TestMethod]
        public void Cisco_881_Att_Router_Import()
        {
            Template template = NewTemplate(TestConstants.Cisco881Att);

            template = _formatter.Read(template);

            Approvals.Approve(ToText(template));
        }

        [TestMethod]
        public void Cisco_881_Sprint_Router_Import()
        {
            Template template = NewTemplate(TestConstants.Cisco881Sprint);

            template = _formatter.Read(template);

            Approvals.Approve(ToText(template));
        }

        [TestMethod]
        public void Cisco_881_Verizon_Router_Import()
        {
            Template template = NewTemplate(TestConstants.Cisco881Verizon);

            template = _formatter.Read(template);

            Approvals.Approve(ToText(template));
        }

        [TestMethod]
        public void Cisco_Head_End_Router_Import()
        {
            Template template = NewTemplate(TestConstants.CiscoHeadend);

            template = _formatter.Read(template);

            Approvals.Approve(ToText(template));
        }
    }
}
